package com.pospi.pospihold.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.util.DoubleSave;

import java.util.List;

/**
 * Created by Qiyan on 2016/6/13.
 */
public class ScanReceiptAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsDto> goodsDtos;

    public ScanReceiptAdapter(Context context, List<GoodsDto> goodsDtos) {
        this.context = context;
        this.goodsDtos = goodsDtos;
    }

    @Override
    public int getCount() {
        return goodsDtos.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsDtos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.scan_receipt_item, null);
            holder.code = (TextView) convertView.findViewById(R.id.tv_pay_code);
            holder.name = (TextView) convertView.findViewById(R.id.tv_good_name);
            holder.price = (TextView) convertView.findViewById(R.id.tv_price);
            holder.num = (TextView) convertView.findViewById(R.id.tv_num);
            holder.discount = (TextView) convertView.findViewById(R.id.tv_discount);
            holder.total = (TextView) convertView.findViewById(R.id.tv_sum);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GoodsDto dto = goodsDtos.get(position);
        holder.code.setText(dto.getCode());
        holder.name.setText(dto.getName());
        holder.price.setText(String.valueOf(dto.getPrice()));
        holder.discount.setText(String.valueOf(dto.getDiscount()));
        holder.num.setText(String.valueOf(dto.getNum()));
        holder.total.setText(String.valueOf(DoubleSave.doubleSaveTwo(dto.getNum() * dto.getPrice() - dto.getDiscount())));
        return convertView;
    }

    class ViewHolder {
        TextView code;
        TextView name;
        TextView price;
        TextView num;
        TextView discount;
        TextView total;
    }
}
